const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';

const listOrders = {};

listOrders.OrderStatus = Object.freeze({
  All: Symbol('All'),
  PendingAvailability: Symbol('PendingAvailability'),
  Pending: Symbol('Pending'),
  Unshipped: Symbol('Unshipped'),
  PartiallyShipped: Symbol('PartiallyShipped'),
  Shipped: Symbol('Shipped'),
  Canceled: Symbol('Canceled'),
  Unfulfillable: Symbol('Unfulfillable'),
});
listOrders.FulfillmentChannel = Object.freeze({
  All: Symbol('All'),
  AFN: Symbol('AFN'),
  MFN: Symbol('MFN'),
});
listOrders.PaymentMethod = Object.freeze({
  All: Symbol('All'),
  COD: Symbol('COD'), // Cash on delivery !valid only in Japan
  CVS: Symbol('CVS'), // Convenience store payment !valid only in Japan
  Other: Symbol('Other'), // Any payment method other than COD or CVS
});
listOrders.EasyShipShipmentStatus = Object.freeze({
  PendingPickUp: Symbol('PendingPickUp'),
  LabelCanceled: Symbol('LabelCanceled'),
  PickedUp: Symbol('PickedUp'),
  OutForDelivery: Symbol('OutForDelivery'),
  Damaged: Symbol('Damaged'),
  Delivered: Symbol('Delivered'),
  RejectedByBuyer: Symbol('RejectedByBuyer'),
  Undeliverable: Symbol('Undeliverable'),
  ReturnedToSeller: Symbol('ReturnedToSeller'),
  ReturningToSeller: Symbol('ReturningToSeller'),
  Lost: Symbol('Lost'),
});
listOrders.generateOption = function(seller_id, mws_auth_token, marketplaceIdList, orderStatusList=null,
                            createdAfter=null, createdBefore=null, lastUpdatedAfter=null, lastUpdatedBefore=null,
                            sellerOrderId=null, buyerEmail=null, maxResultsPerPage=null, fulfillmentChannelList=null,
                            paymentMethodList=null,tfmShipmentStatusList=null){
  let baseOption = {
    'Version': '2013-09-01',
    'Action': 'ListOrders',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
    // 'MarketplaceId.Id.1': 'MARKET_PLACE_ID_1',
    // 'OrderStatus.Status.1': 'Pending',
    // 'OrderStatus.Status.2': 'Canceled'
  };

  if (createdAfter)
    baseOption['CreatedAfter'] = createdAfter;
  if (createdBefore)
    baseOption['CreatedBefore'] = createdBefore;
  if (lastUpdatedAfter)
    baseOption['LastUpdatedAfter'] = lastUpdatedAfter;
  if (lastUpdatedBefore)
    baseOption['LastUpdatedBefore'] = lastUpdatedBefore;
  if (sellerOrderId)
    baseOption['SellerOrderId'] = sellerOrderId;
  if (buyerEmail)
    baseOption['BuyerEmail'] = buyerEmail;
  if (maxResultsPerPage)
    baseOption['MaxResultsPerPage'] = maxResultsPerPage;

  if (marketplaceIdList) {
    let keyPrefix = "MarketplaceId.Id";
    marketplaceIdList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item;
    });
  }

  if (orderStatusList) {
    let keyPrefix = "OrderStatus.Status";
    orderStatusList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item.description;
    });
  }
  if (fulfillmentChannelList) {
    let keyPrefix = "FulfillmentChannel.Channel";
    fulfillmentChannelList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item.description;
    });
  }
  if (paymentMethodList) {
    let keyPrefix = "PaymentMethod.Method";
    paymentMethodList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item.description;
    });
  }
  if (tfmShipmentStatusList) {
    let keyPrefix = "TFMShipmentStatus.Status";
    tfmShipmentStatusList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item.description;
    });
  }
  return baseOption;
};

listOrders._ = function(options) {
  if (!isProduction) {
    return amazonMws.orders.search(options);
  } else {
    console.error("bot not implemented!")
  }
};

listOrders.listOrdersAll = function(seller_id, mws_auth_token, marketplaceIdList) {
    return this._(this.generateOption(seller_id, mws_auth_token, marketplaceIdList, null, createdAfter=new Date(2000, 1, 1)));
};

module.exports = listOrders;